package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OutboxService {

	@Autowired
	private OutboxDao outboxDao;

	private QOutbox $ = QOutbox.outbox;

	@Autowired
	public void setOutboxDao(OutboxDao outboxDao) {
		this.outboxDao = outboxDao;
	}

	public List<Outbox> findAll() {
		return ListUtil.toList(outboxDao.findAll());
	}

	public List<Outbox> findAll(Long personId, Long receiverId,
			Date sendTimeBegin, Date sendTimeEnd, String messageContent,
			String status, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		if (IdUtil.isNotEmpty(receiverId)) {
			condition.and($.receiver.id.eq(receiverId));
		}

		if (sendTimeBegin != null) {
			condition.and($.sendTime.after(sendTimeBegin));
		}

		if (sendTimeEnd != null) {
			condition.and($.sendTime.before(sendTimeEnd));
		}

		if (StringUtil.isNotEmpty(messageContent)) {
			condition.and($.messageContent.contains(messageContent));
		}

		if (StringUtil.isNotEmpty(status)) {
			condition.and($.status.contains(status));
		}

		return ListUtil.toList(outboxDao.findAll(condition));
	}

	public Page<Outbox> findAll(Long personId, Long receiverId,
			Date sendTimeBegin, Date sendTimeEnd, String messageContent,
			String status, int pageNumber, int pageSize, String sortAttribute,
			String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}
		if (IdUtil.isNotEmpty(receiverId)) {
			condition.and($.receiver.id.eq(receiverId));
		}
		if (sendTimeBegin != null) {
			condition.and($.sendTime.after(sendTimeBegin));
		}

		if (sendTimeEnd != null) {
			condition.and($.sendTime.before(sendTimeEnd));
		}
		if (StringUtil.isNotEmpty(messageContent)) {
			condition.and($.messageContent.contains(messageContent));
		}
		if (StringUtil.isNotEmpty(status)) {
			condition.and($.status.contains(status));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return outboxDao.findAll(condition, pageRequest);
	}

	public Outbox getOutbox(Long id) {
		return outboxDao.getOne(id);
	}

	public void deleteOutbox(Long id) {
		outboxDao.delete(id);
	}

	public void saveOutbox(Outbox outbox) {
		if(null == outbox.getSendTime()){
			outbox.setSendTime(DateUtil.nowDate());
		}
		outboxDao.save(outbox);
	}
	public Outbox findOne(Long id) {
		return this.outboxDao.findOne(id);
	}

}
